package com.eonmind.vip.mapper;


import com.eonmind.common.contract.data.base.BaseMongoMapper;
import com.eonmind.common.contract.enums.DelEnum;
import com.eonmind.common.provider.entity.vip.PointsConsumption;
import com.eonmind.user.contract.dto.UserInfoData;
import com.eonmind.vip.dto.ConsumptionDto;
import com.mongoplus.conditions.query.QueryWrapper;
import com.mongoplus.model.PageResult;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

@Component
public class PointsMapper extends BaseMongoMapper<PointsConsumption> {

    public PageResult<PointsConsumption> page(UserInfoData userInfoData, ConsumptionDto.ConsumptionPageRequest pageRequest) {
        if (ObjectUtils.isEmpty(pageRequest)) {
            return new PageResult<>();
        }
        QueryWrapper<PointsConsumption> mgoQuery = createMgoQuery()
                .eq(PointsConsumption::getUserID, userInfoData.getId())
                .between(ObjectUtils.isNotEmpty(pageRequest.getStartTime()) && pageRequest.getStartTime() != 0, PointsConsumption::getCreateTime, pageRequest.getStartTime(), pageRequest.getEndTime(), true)
                .eq(ObjectUtils.isNotEmpty(pageRequest.getKeyword()) && !Objects.equals(pageRequest.getKeyword(), "全部"), PointsConsumption::getCategory, pageRequest.getKeyword())
                .eq(PointsConsumption::getDel, DelEnum.UN_DEL.getCode());

        return page(mgoQuery, pageRequest.getPage());
    }


    public Integer gePointsByMonth(Long userId, long startTime, long endTime, String type) {
        QueryWrapper<PointsConsumption> queryWrapper = createMgoQuery()
                .eq(PointsConsumption::getUserID, userId)
                .gt(PointsConsumption::getCreateTime, startTime)
                .lt(PointsConsumption::getCreateTime, endTime)
                .eq(PointsConsumption::getCategory, type);
        List<PointsConsumption> list = list(queryWrapper);
        int sum = 0;
        for (PointsConsumption pointsConsumption: list) {
            sum += pointsConsumption.getConsumption();
        }
return  sum;


    }
}
